package com.clone.surpassli.clone_cloudreader.ui.common;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.clone.surpassli.clone_cloudreader.R;
import com.clone.surpassli.clone_cloudreader.utils.CommonUtils;
import com.clone.surpassli.clone_cloudreader.utils.statusbar.StatusBarUtil;

/**
 * Created by SurpassLi on 2017/9/30.
 * WebViewActivity
 */
public class WebViewActivity extends AppCompatActivity implements IWebPageView {

    // 进度条
    ProgressBar mProgressBar;
    WebView mWebView;

    // 全屏时视频加载view
    FrameLayout videoFullView;
    Toolbar mTitleToolBar;

    // 进度条是否加载到90%
    public boolean mProgress90;

    // 网页是否加载完成
    public boolean mPageFinish;

    // 加载视频相关
//    private MyWebChromeClient mWebChromeClient;

    // title
    private String mTitle;
    // 网页链接
    private String mUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        getIntentData();
        initTitle();
        initWebView();
        mWebView.loadUrl(mUrl);
    }

    private void initWebView() {
        mProgressBar.setVisibility(View.VISIBLE);
        WebSettings webSetting = mWebView.getSettings();
        // 网页内容的宽度是否可大于WebView控件的宽度
        webSetting.setLoadWithOverviewMode(false);
        // 保存表单数据
        webSetting.setSaveFormData(true);
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setDisplayZoomControls(true);
        //启动应用缓存
        webSetting.setAppCacheEnabled(true);
        // 设置缓存模式为默认
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 设置此属性，可任意比例缩放。
        webSetting.setUseWideViewPort(true);
        // 缩放比例 1
        mWebView.setInitialScale(1);
        // 告诉WebView启用JavaScript执行，默认的是false。
        webSetting.setJavaScriptEnabled(true);
        // 页面加载好以后，再放开图片
        webSetting.setBlockNetworkImage(true);
        // 使用localStorage则必须打开
        webSetting.setDomStorageEnabled(true);
        // 排版适应屏幕
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        // WebView是否支持多个窗口。
        webSetting.setSupportMultipleWindows(true);
        // webView从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        //设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)
        webSetting.setTextZoom(100);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                mProgressBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }
        });
    }

    private void initTitle() {
        StatusBarUtil.setColor(this, CommonUtils.getColor(R.color.colorTheme), 0);
        mProgressBar = (ProgressBar) findViewById(R.id.pb_web_view);
        mWebView = (WebView) findViewById(R.id.wv_web_view);
        mTitleToolBar = (Toolbar) findViewById(R.id.tb_web_view);
        videoFullView = (FrameLayout) findViewById(R.id.video_fullView);
        setSupportActionBar(mTitleToolBar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        setTitle(mTitle);
        mTitleToolBar.setOverflowIcon(ContextCompat.getDrawable(this,R.drawable.actionbar_more));
        mTitleToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    @Override
    public void hindProgressBar() {

    }

    @Override
    public void showWebView() {

    }

    @Override
    public void hindWebView() {

    }

    @Override
    public void startProgress() {

    }

    @Override
    public void progressChanged(int newProgress) {

    }

    @Override
    public void addImageClickListener() {

    }

    @Override
    public void fullViewAddView(View view) {

    }

    @Override
    public void showVideoFullView() {

    }

    @Override
    public void hindVideoFullView() {

    }

    private void getIntentData() {
        if (getIntent() != null) {
            mTitle = getIntent().getStringExtra("mTitle");
            mUrl = getIntent().getStringExtra("mUrl");
        }
    }

    public void setTitle(String mTitle) {
        mTitleToolBar.setTitle(mTitle);
        mTitleToolBar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
    }

    /**
     * 跳WebView页，这样写的确可以减少代码量
     * @param mContext Context
     * @param mUrl String
     * @param mTitle String
     */
    public static void loadUrl(Context mContext, String mUrl, String mTitle) {
        Intent intent = new Intent(mContext, WebViewActivity.class);
        intent.putExtra("mTitle",mTitle);
        intent.putExtra("mUrl",mUrl);
        mContext.startActivity(intent);
    }
}